﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace HDJ.Framework.Core
{
    public static class UnityExtendUtils
    {
        /// <summary>
        /// 获取GameObject的子物体
        /// </summary>
        /// <param name="root"></param>
        /// <param name="findName">当不为空时寻找当前名字的物体，当为空时返回所有子物体</param>
        /// <returns></returns>
        public static Transform[] GetDeepChildTransforms(Transform root,string findName="")
        {
            List<Transform> tempList = new List<Transform>();
            for (int i = 0; i < root.childCount; i++)
            {
                Transform temp = root.GetChild(i);
                if (string.IsNullOrEmpty(findName))
                {
                    tempList.Add(temp);
                }
                else
                {
                    if (temp.name == findName)
                    {
                        tempList.Add(temp);
                    }
                }
                tempList.AddRange(GetDeepChildTransforms(temp, findName));
            }

            return tempList.ToArray();
        }

        /// <summary>
        /// 根据不同平台返回文件夹名字
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static string GetPlatformFolderName(RuntimePlatform target)
        {
            switch (target)
            {
                case RuntimePlatform.Android:
                    return "Android";
                case RuntimePlatform.IPhonePlayer:
                    return "IOS";
                case RuntimePlatform.WebGLPlayer:
                    return "WebGL";
                case RuntimePlatform.WindowsEditor:
                case RuntimePlatform.WindowsPlayer:
                    return "Windows";
                case RuntimePlatform.OSXEditor:
                case RuntimePlatform.OSXPlayer:
                    return "OSX";
                case RuntimePlatform.LinuxEditor:
                case RuntimePlatform.LinuxPlayer:
                    return "Linux";
                case RuntimePlatform.tvOS:
                    return "Apple_TvOS";
                case RuntimePlatform.XboxOne:
                    return "XboxOne";
                //case RuntimePlatform.PSP2:
                //    return "PS_Vita";
                case RuntimePlatform.PS4:
                    return "PS4";
                case RuntimePlatform.WSAPlayerARM:
                case RuntimePlatform.WSAPlayerX64:
                case RuntimePlatform.WSAPlayerX86:
                    return "WindowsStoreApps";

                default:
                    return null;
            }
        }

        public static string GetWWWLoadPath(string path)
        {
#if UNITY_EDITOR
            path = @"file:///" + path;
#elif UNITY_IOS
        path = @"file://" + path;
#else
        
#endif
            return path;
        }

        /// <summary>
        /// 创建GameObject并附上脚本
        /// </summary>
        /// <typeparam name="T">脚本</typeparam>
        /// <param name="ObejectName">GameObject名字</param>
        /// <param name="DontDestroyOnLoad">是否切换场景时不摧毁</param>
        /// <param name="isFindFirst">是否先寻找是否存在同名物体</param>
        /// <returns></returns>
        public static T CreateMonoBehaviourBase<T>(string ObejectName = "", bool DontDestroyOnLoad = true, bool isFindFirst = false) where T : MonoBehaviour
        {
            if (string.IsNullOrEmpty(ObejectName))
                ObejectName = "[" + typeof(T).Name + "]";
            GameObject obj = null;
            if (isFindFirst)
            {
                obj = GameObject.Find(ObejectName);
            }
            if (obj == null)
                obj = new GameObject(ObejectName);

            if (DontDestroyOnLoad && Application.isPlaying)
                UnityEngine.Object.DontDestroyOnLoad(obj);
            T t = obj.GetComponent<T>();
            if (t == null)
                t = obj.AddComponent<T>();
            return t;
        }
    }
}
